package com.hospital.controller;

import com.hospital.dao.UserDao;
import com.hospital.domain.Department;
import com.hospital.domain.Doctor;
import com.hospital.domain.User;
import com.hospital.service.DoctorService;
import com.hospital.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    DoctorService doctorService;

    @Autowired
    UserDao userDao;

    @GetMapping("/selectAll")
    public Result selectAll(){
        List<User> users = userService.selectAll();
        System.out.println(users);
        return new Result(Code.GET_OK,users,"查询成功");
    }

    @PostMapping("/selectAllDoctor")
    public Result selectAllDoctor(@RequestBody Doctor doctor){
        String doctorName = doctor.getDoctorName();
        String departmentName = doctor.getDepartmentName();
        List<Doctor> doctors = doctorService.selectDoctorByCondition(doctorName,departmentName);
        return new Result(Code.GET_OK,doctors,"查询成功");
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user,HttpServletRequest request,HttpServletResponse response) throws IOException {

        String accountNum = user.getAccountNum();
        String userPWD = user.getPwd();
        return userService.login(accountNum,userPWD,request,response);
    }

    @PostMapping("selectUser")
    public User selectUser(HttpSession session){
        int userID = (int)session.getAttribute("userID");
        return userDao.selectUser(userID);
    }

    @PostMapping("/selectAllDep")
    public List<Department> selectAllDep(){
        List<Department> departments = userService.selectAllDep();
        return departments;
    }

//    @PostMapping("/addUser")
//    public String addUser(@RequestBody User user){
//        System.out.println(user);
//        boolean res = userService.addUser(user);
//        if(res) {
//            return "success";
//        }else{
//            return "error";
//        }
//    }

    @PostMapping("/updateUser")
    public int updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user,HttpSession session){
        System.out.println(user);
        String  accountNum = user.getAccountNum();
        User u = userService.selectUserByAccount(accountNum);
        if(u != null) {
            return new Result(0,null,"已存在该账户换一个");
        }
        int n = userService.addUser(user);
        if(n==1){
            User u2 = userService.selectUserByAccount(user.getAccountNum());
            session.setAttribute("userID",u2.getUserID());
            session.setAttribute("userAccountNum",u2.getAccountNum());
            return new Result(1,user,"注册成功");
        }else{
            return new Result(0,null,"注册失败");
        }
    }

}
